include:
  - local: .gitlab/ci/qa-common/main.gitlab-ci.yml
  - template: Verify/Browser-Performance.gitlab-ci.yml
  - component: "gitlab.com/gitlab-org/quality/pipeline-common/allure-report@7.3.0"
    inputs:
      job_name: "e2e-test-report"
      job_stage: "post-qa"
      aws_access_key_id_variable_name: "QA_ALLURE_AWS_ACCESS_KEY_ID"
      aws_secret_access_key_variable_name: "QA_ALLURE_AWS_SECRET_ACCESS_KEY"
      gitlab_auth_token_variable_name: "PROJECT_TOKEN_FOR_CI_SCRIPTS_API_USAGE"
      allure_results_glob: "qa/tmp/allure-results"
      allure_ref_slug: "${CI_COMMIT_REF_SLUG}"
      allure_project_path: "${CI_PROJECT_PATH}"
      allure_merge_request_iid: "${CI_MERGE_REQUEST_IID}"
      allure_job_name: "${QA_RUN_TYPE}"

.test-variables:
  variables:
    QA_GENERATE_ALLURE_REPORT: "true"
    QA_CAN_TEST_PRAEFECT: "false"
    GITLAB_USERNAME: "root"
    GITLAB_PASSWORD: "${REVIEW_APPS_ROOT_PASSWORD}"
    GITLAB_ADMIN_USERNAME: "root"
    GITLAB_ADMIN_PASSWORD: "${REVIEW_APPS_ROOT_PASSWORD}"
    GITLAB_QA_ADMIN_ACCESS_TOKEN: "${REVIEW_APPS_ROOT_TOKEN}"
    GITHUB_ACCESS_TOKEN: "${QA_GITHUB_ACCESS_TOKEN}"

.bundle-base:
  extends:
    - .qa-cache
    - .ruby-image
  before_script:
    - cd qa && bundle install

.review-qa-base:
  image: ${REGISTRY_HOST}/${REGISTRY_GROUP}/gitlab-build-images/debian-${DEBIAN_VERSION}-ruby-${RUBY_VERSION}:bundler-2.3-git-2.36-lfs-2.9-chrome-${CHROME_VERSION}-docker-${DOCKER_VERSION}-gcloud-383-kubectl-1.23
  extends:
    - .use-docker-in-docker
    - .bundle-base
    - .test-variables
  stage: qa
  needs:
    - review-deploy
    - download-knapsack-report
  variables:
    GIT_LFS_SKIP_SMUDGE: 1
    WD_INSTALL_DIR: /usr/local/bin
    RSPEC_REPORT_OPTS: --force-color --order random --format documentation --format RspecJunitFormatter --out tmp/rspec-${CI_JOB_ID}.xml
  script:
    - QA_COMMAND="bundle exec bin/qa ${QA_SCENARIO} ${QA_GITLAB_URL} -- ${QA_TESTS} ${RSPEC_REPORT_OPTS}"
    - echo "Running - '${QA_COMMAND}'"
    - eval "$QA_COMMAND"
  after_script:
    - |
      echo "Sentry errors for the current review-app test run can be found via following url:"
      echo "https://sentry.gitlab.net/gitlab/gitlab-review-apps/releases/$(echo "${CI_COMMIT_SHA}" | cut -c1-11)/all-events/."
  artifacts:
    paths:
      - qa/tmp
    reports:
      junit: qa/tmp/rspec-*.xml
    expire_in: 7 days
    when: always

# Store knapsack report as artifact so the same report is reused across all jobs
download-knapsack-report:
  extends:
    - .bundle-base
    - .rules:prepare-report
  stage: prepare
  script:
    - bundle exec rake "knapsack:download[qa]"
  allow_failure: true
  artifacts:
    paths:
      - qa/knapsack/review-qa-*.json
    expire_in: 1 day

review-qa-smoke:
  extends:
    - .review-qa-base
    - .rules:qa-smoke
  variables:
    QA_SCENARIO: Test::Instance::Smoke

review-qa-blocking:
  extends:
    - .review-qa-base
    - .rules:qa-blocking
  variables:
    QA_SCENARIO: Test::Instance::ReviewBlocking
  retry: 1
review-qa-blocking-parallel:
  extends:
    - review-qa-blocking
    - .rules:qa-blocking-parallel
  parallel: 10

review-qa-non-blocking:
  extends:
    - .review-qa-base
    - .rules:qa-non-blocking
  variables:
    QA_SCENARIO: Test::Instance::ReviewNonBlocking
  when: manual
  allow_failure: true
review-qa-non-blocking-parallel:
  extends:
    - review-qa-non-blocking
    - .rules:qa-non-blocking-parallel
  parallel: 5

browser_performance:
  extends:
    - .default-retry
    - .review:rules:review-performance
  stage: qa
  needs: ["review-deploy"]
  variables:
    URL: environment_url.txt

e2e-test-report:
  extends: .rules:prepare-report

upload-knapsack-report:
  extends:
    - .generate-knapsack-report-base
    - .bundle-base
  stage: post-qa
  variables:
    QA_KNAPSACK_REPORT_FILE_PATTERN: $CI_PROJECT_DIR/qa/tmp/knapsack/*/*.json

delete-test-resources:
  extends:
    - .bundle-base
    - .rules:prepare-report
  stage: post-qa
  variables:
    GITLAB_QA_ACCESS_TOKEN: $REVIEW_APPS_ROOT_TOKEN
  script:
    - export GITLAB_ADDRESS="$QA_GITLAB_URL"
    - bundle exec rake "test_resources:delete[$CI_PROJECT_DIR/qa/tmp/test-resources-*.json]"
  allow_failure: true

notify-slack:
  extends:
    - .notify-slack
    - .rules:main-run
  stage: post-qa
  variables:
    QA_RSPEC_XML_FILE_PATTERN: ${CI_PROJECT_DIR}/qa/tmp/rspec-*.xml
    RUN_WITH_BUNDLE: "true"
  when: on_failure

export-test-metrics:
  extends:
    - .export-test-metrics
    - .bundle-base
    - .rules:main-run
  stage: post-qa
  variables:
    QA_METRICS_REPORT_FILE_PATTERN: tmp/test-metrics-*.json
  when: always
